<?php
class tvController extends  appController{
	public function index()
	{
		$this->assign("now", yf_time());
		$this->display('index.html');
	}
	
	public function record()
	{
		$access_token = $this->getYaoAccessToken();
		$time = date('Ymd',strtotime($_REQUEST['now']));
		$params = "?access_token=$access_token&date=$time";
		$url = "https://api.weixin.qq.com/yaotv/program/query.json";
		$json = file_get_contents($url.$params);//获取微信用户基本信息
		$arr = json_decode($json,true);
// 		print_r()
		$this->assign("errcode", $arr['errcode']);
		$this->assign("errmsg", $arr['errmsg']);
		$this->assign('art_list', $arr['data']['programs']);
		$this->display("record.html");
	}
	
	public function series()
	{
		$access_token = $this->getYaoAccessToken();
		$time = date('Ymd',strtotime($_REQUEST['now']));
// 		$params = "&date=$time";
		$url = "https://api.weixin.qq.com/yaotv/series/query.json?access_token=$access_token";
		$json = file_get_contents($url.$params);//获取微信用户基本信息
		$arr = json_decode($json,true);
		// 		print_r()
		$this->assign("errcode", $arr['errcode']);
		$this->assign("errmsg", $arr['errmsg']);
		$this->assign('art_list', $arr['data']['records']);
		$this->display("record_series.html");
	}
	
	/**
	 * 电视预约
	 */
	public function addTvOrder()
	{
		$reserveId = isset($_REQUEST['reserveId'])?intval($_REQUEST['reserveId']):0;
		if(!$reserveId){
			$result = array("errcode"=>1,"errmsg"=>"预约Id为空");
			$this->showMsg($result);
		}
		$access_token = $this->getYaoAccessToken();
		$url = "https://api.weixin.qq.com/yaotv/program/reserve/new/add.json?access_token=$access_token";
		$arr = array("reserveId"=>reserveId,"msgIconUrl"=>"","detailUrl"=>"");
		$res = json_decode($this->post_it($url,json_encode($arr)),true);
		$this->showMsg($res);
	}
	
	public function getYaoAccessToken()
	{
		$path = ROOT_PATH."Qiaqia/Json/yao_access_token.json";
		$data = json_decode(file_get_contents($path), true);
		if(empty($data['access_token']) || $data['expire_time'] < yf_time()){
			$path2 = ROOT_PATH."Admin/Config/yaotv.json";
			$data2 = json_decode(file_get_contents($path2), true);
			$app['appid'] = $data2['yaoappid'];
			$app['appsecret'] = $data2['yaoappSecret'];
			$new_info = $this->getToken($app['appid'], $app['appsecret']);
			$info['access_token'] = $new_info['access_token'];
			$info['expire_time'] = yf_time() + 1500;
			$fp = fopen($path, "w");
			fwrite($fp, json_encode($info));
			fclose($fp);
			return $new_info['access_token'];
		}else{
			return $data['access_token'];
		}
	}
	public function getToken($appid,$secret){
		$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($ch);
		$token = json_decode($result);
		$yf_info['access_token'] = $token->access_token;
		$yf_info['expires_in'] = $token->expires_in;
		return $yf_info;
	}
	
	/**
	 * 用post方法获取数据
	 * @param $url
	 * @param $data
	 * @return
	 */
	public function post_it($url, $data)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		'Content-Type: application/json; charset=utf-8',
		'Content-Length: ' . strlen($data))
		);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$result = curl_exec($curl);
		if($result){
			curl_close($curl);
			return $result;
		}
		else {
			$error = curl_errno($curl);
			curl_close($ch);
			return false;
		}
	}
	
}